package com.shyroke.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shyroke.entity.Article;
import com.shyroke.entity.ArticlePingLun;
import com.shyroke.entity.User;
import com.shyroke.service.ArticlePingLunServiceI;
import com.shyroke.service.ArticleServiceI;

@Controller
@RequestMapping(value="/articleAction")
public class ArticleAction {

	@Autowired
	private ArticleServiceI articleService;
	
	@Autowired
	private ArticlePingLunServiceI articlePingLunService;
	
	/**
	 * 显示出每个标签可以在首页中显示的文章
	 * @param labelId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/labelView")
	public ModelAndView labelView(@RequestParam(value="labelid",required=false)Integer labelId,ModelAndView model) {
		
		List<Article> articleList=articleService.selectShowArticleByLabelId(labelId);
		model.addObject("articleList", articleList);
		model.setViewName("article/showLabelArticle");
		
		
		return model;
	}
	
	/**
	 * 保存用户评论
	 * @param content 评论内容
	 * @param session
	 * @param articleId 被评论文章的id
	 * @return
	 */
	@RequestMapping(value="/pinglun")
	@ResponseBody
	public Map<String,Object> pinglun(@RequestParam(value="content")String content,HttpSession session,
			@RequestParam(value="articleId")Integer articleId){
		Map<String,Object> map=new HashMap<>();
		User user=(User)session.getAttribute("user");
		
		if(user==null) {
			//未登录用户不能评论
			map.put("errMsg", "请先登录再评论");
		}else {
			ArticlePingLun articlepinglun=new ArticlePingLun();
			articlepinglun.setArticleid(articleId);
			articlepinglun.setContent(content);
			articlepinglun.setUserid(user.getId());
			articlePingLunService.insert(articlepinglun);
			map.put("msg", "评论成功");
		}
		
		
		return map;
				
	}
	
	
	/**
	 * 跳转到图书管理的页面
	 * @return
	 */
	@RequestMapping(value="/articleMainView")
	public String articleMainView() {
		return "admin/articleMain";
	}
	
	
	@RequestMapping(value="/getarticleDataGrid")
	@ResponseBody
	public Map<String,Object> getarticleDataGrid(@RequestParam(value="author",required=false)String author){
		Map<String,Object> map=new HashMap<>();
		List<Article> articleList = null; //未审核文章集合
		if(author != null) {
			//查找某个作者的未审核文章
			articleList=articleService.selectNoShenHeByAuthor(author);
		}else {
			 articleList=articleService.selectAllNoShenHe();
		}
	
		List<Map<String,Object>> dataList=new ArrayList<>();
		Map<String,Object> columnMap = null;
		for(Article article:articleList) {
			columnMap = new HashMap<>();
			columnMap.put("id", article.getId());
			columnMap.put("title", article.getTitle());
			columnMap.put("author", article.getAuthor());
			dataList.add(columnMap);
		} 
		
		map.put("total", articleList.size());
		map.put("rows", dataList);
		return map;
	}
	
	/**
	 * 跳转到查看某篇文章的内容
	 * @param model
	 * @param articleId 文章id
	 * @return
	 */
	@RequestMapping(value="/lookContentView")
	public  ModelAndView lookContentView(ModelAndView model,@RequestParam(value="articleId",required=true)Integer articleId) {
		Article article=articleService.selectById(articleId);
		model.addObject("article", article);
		model.setViewName("article/contentDesc");
		return model;
	}
	
	@RequestMapping(value="/shenhe")
	@ResponseBody
	public Map<String,Object> shenhe(@RequestParam(value="articleId",required=true)Integer articleId){
		Map<String,Object> map=new HashMap<>();
			
		Article article=articleService.selectById(articleId);
		article.setIsshenhe("是");
		article.setIsshow("是");
		articleService.uptArticle(article);
		
		map.put("msg", "审核成功");
		return map;
		}
}
